using System;
using System.Windows.Forms;
using SnarlGMailNotifier.Classes;
using SnarlGMailNotifier.Properties;

namespace SnarlGMailNotifier.Forms
{
    public partial class FSettings : Form
    {
        // private BrowserFetcher m_browserFetcher;
        private readonly string UNIQUE_AUTORUN_NAME = Application.ProductName + "_" + Utility.UniqueApplicationID.ToString();

        public FSettings()
        {
            InitializeComponent();
        }

        private void FSettings_Load(object sender, EventArgs e)
        {
            #region Fill Browserlist
            //m_browserFetcher = new BrowserFetcher();

            ImageList imgList = new ImageList();
            //lstBrowsers.SmallImageList = imgList;

            //int i = 0;

            /*            foreach (BrowserItem browser in m_browserFetcher.BrowserItems)
                        {
                            imgList.Images.Add(browser.IconDetails.Icon);

                            ListViewItem lvItem = null;
                
                            lvItem = new ListViewItem(browser.BrowserName);
                            lvItem.SubItems.Add(browser.Command);
                            lvItem.ImageIndex = i;
                            lvItem.Tag = browser;

            //                lstBrowsers.Items.Add(lvItem);

                            i++;
                        }*/

            //lstBrowsers.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            #endregion
        }

        private void FSettings_Shown(object sender, EventArgs e)
        {
            #region Load Tab "Common"
            txtUsername.Text = Settings.Default.GMailUsername.Trim();
            txtPassword.Text = Settings.Default.GMailPassword.Trim();
            //hintTxtUsername.InternalText = Settings.Default.GMailUsername.Trim();
            //hintTxtPassword.InternalText = Settings.Default.GMailPassword.Trim();
            chkSavePassword.Checked = Settings.Default.SavePassword;

            numNewMailCheckInterval.Value = Settings.Default.NewMailCheckIntervalSeconds;

            chkAutorun.Checked = RegistryAutorun.AutorunExists(UNIQUE_AUTORUN_NAME);
            #endregion

            #region Load Tab "Browser"
            /* foreach (ListViewItem lvItem in lstBrowsers.Items)
            {
                if (Settings.Default.CurrentBrowser == ((BrowserItem)lvItem.Tag).BrowserKey)
                {
                    lvItem.Selected = true;
                    lvItem.Focused = true;
                    lvItem.EnsureVisible();
                    lstBrowsers.Select();

                    break;
                }
            }*/
            #endregion
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

            #region Save Tab "Common"
            Settings.Default.SavePassword = chkSavePassword.Checked;
            Settings.Default.GMailUsername = txtUsername.Text.Trim();
            //Settings.Default.GMailUsername = hintTxtUsername.InternalText.Trim();

            if (chkSavePassword.Checked)
            {
                Settings.Default.GMailPassword = txtPassword.Text.Trim();
                //Settings.Default.GMailPassword = hintTxtPassword.InternalText.Trim();
            }
            else
            {
                Settings.Default.GMailPassword = "";
            }
            if (chkAutorun.Checked)
            {
                RegistryAutorun.SetAutorun(UNIQUE_AUTORUN_NAME, Application.ExecutablePath);
            }
            else
            {
                RegistryAutorun.RemoveAutorun(UNIQUE_AUTORUN_NAME);
            }

            Settings.Default.NewMailCheckIntervalSeconds = Convert.ToUInt32(numNewMailCheckInterval.Value);

            #endregion

            #region Save Tab "Browser"
            //  if (lstBrowsers.SelectedItems.Count > 0)
            //      Settings.Default.CurrentBrowser = ((BrowserItem)lstBrowsers.SelectedItems[0].Tag).BrowserKey;
            #endregion

            Settings.Default.Save();

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSetDefaultBrowser_Click(object sender, EventArgs e)
        {
            /*  foreach (ListViewItem lvItem in lstBrowsers.Items)
              {
                  if ((BrowserItem)lvItem.Tag == m_browserFetcher.DefaultBrowser)
                  {
                      lvItem.Selected = true;
                      lvItem.Focused = true;
                      lvItem.EnsureVisible();
                      lstBrowsers.Select();
                      break;
                  }
              }*/
        }
    }
}